package com.itheima.sc.impl.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsFlashProductRelationApi;

import com.itheima.sc.config.ComException;
import com.itheima.sc.mapper.pms.ProductMapper;
import com.itheima.sc.mapper.sms.SmsFlashProductRelationMapper;
import com.itheima.sc.pojo.pms.Product;
import com.itheima.sc.pojo.sms.FlashPromotionProductRelation;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@DubboService
public class SmsFlashProductRelationImpl implements SmsFlashProductRelationApi {

    @Autowired
    private SmsFlashProductRelationMapper relationMapper;
    @Autowired
    private ProductMapper productMapper;

    //批量添加秒杀关系
    public void addRelations(List<FlashPromotionProductRelation> relations) {
        //遍历添加秒杀的活动关系
        for (FlashPromotionProductRelation relation : relations) {
            relationMapper.insert(relation);
        }
    }

    //删除关联数据
    public void deleteRelation(Long id) {
        relationMapper.deleteById(id);
    }

    //查询关联数据
    public FlashPromotionProductRelation selectRelation(Long id) {
        return relationMapper.selectById(id);
    }

    //分页查询不同场次关联及商品信息
    public PageResult selectRelationBySession(int page, int pageSize, int flashPromotionId, int sessionId) {
        //构建分页查询
        Page<FlashPromotionProductRelation> ipage = new Page<>();
        LambdaQueryWrapper<FlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        wrapper.eq(FlashPromotionProductRelation::getFlashPromotionSessionId,sessionId);
        //排序
        wrapper.orderByDesc(FlashPromotionProductRelation::getSort);
        ipage = relationMapper.selectPage(ipage, wrapper);
        //
        for (FlashPromotionProductRelation record : ipage.getRecords()) {
            Long productId = record.getProductId();
            //根据id查询商品信息
            Product product = productMapper.selectById(productId);
            record.setProduct(product);
        }
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //修改关联信息
    public void updateProductRelation(Long id, FlashPromotionProductRelation relation) {
        //1.判断数据是否存在
        boolean flag = ObjectUtil.isNotEmpty(relationMapper.selectById(id)) ? true : false;
        if (flag) {
            //及数据存在 进行需改操作
            relationMapper.updateById(relation);
        }
    }

    //查询商品种类的数量
    public int findRelationCount(Long sessionId) {
        LambdaQueryWrapper<FlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlashPromotionProductRelation::getFlashPromotionSessionId, sessionId);
        List<FlashPromotionProductRelation> relations = relationMapper.selectList(wrapper);
        return relations.size();
    }
}
